<template>
  <el-card class="temreture-analysis-wrapper card-header">
    <header>
      <div class="head">
        <div class="head-icon">
          <svg-icon icon-class="dashboard-1"></svg-icon>
        </div>
        <div>温度趋势分析</div>
      </div>
      <div><XyTimePicker></XyTimePicker></div>
    </header>
    <div class="data-energy-container" ref="chartContainer"></div>
    <div class="analysis-wrapper electricity-consumption">
      <header>温湿度分析</header>
      <main>
        <div class="main-left">
          <div>今日数据:</div>
          <div><svg-icon icon-class="dot"></svg-icon>最高温度:25.2℃(14:30)</div>
          <div><svg-icon icon-class="dot"></svg-icon>最低温度:19.8℃(16:30)</div>
          <div><svg-icon icon-class="dot"></svg-icon>平均温度:22.4℃</div>
          <div><svg-icon icon-class="dot"></svg-icon>最高湿度:62.5%(08:45)</div>
          <div><svg-icon icon-class="dot"></svg-icon>最低湿度:43.2%(15:45)</div>
          <div><svg-icon icon-class="dot"></svg-icon>平均湿度:55.5%</div>
        </div>
        <div class="main-right">
          <div>昨日数据:</div>
          <div><svg-icon icon-class="dot"></svg-icon>最高温度:25.2℃(14:30)</div>
          <div><svg-icon icon-class="dot"></svg-icon>最低温度:19.8℃(16:30)</div>
          <div><svg-icon icon-class="dot"></svg-icon>平均温度:22.4℃</div>
          <div><svg-icon icon-class="dot"></svg-icon>最高湿度:62.5%(08:45)</div>
          <div><svg-icon icon-class="dot"></svg-icon>最低湿度:43.2%(15:45)</div>
          <div><svg-icon icon-class="dot"></svg-icon>平均湿度:55.5%</div>
        </div>
      </main>
    </div>
  </el-card>
</template>
<script>
import XyTimePicker from "@/components/XyTimePicker.vue";
import * as echarts from "echarts";
export default {
  components: {
    XyTimePicker,
  },
  data() {
    return {
      chart: null,
      chartData: {
        xAxis: ["1月", "4月", "7月", "10月", ""],
        // 修改为包含两个数据系列
        series1: [450, 460, 470, 455, 445],
        series2: [620, 732, 425, 734, 580],
        series3: [550, 640, 225, 834, 380],
      },
    };
  },
  mounted() {
    this.initChart();
  },
  beforeDestroy() {
    if (this.chart) {
      this.chart.dispose();
      window.removeEventListener("resize", this.resizeChart);
    }
  },
  methods: {
    initChart() {
      this.$nextTick(() => {
        const chartDom = this.$refs.chartContainer;
        if (chartDom) {
          this.chart = echarts.init(chartDom);
          this.setChartOption();

          window.addEventListener("resize", this.resizeChart);
        }
      });
    },

    setChartOption() {
      const option = {
        tooltip: {
          trigger: "axis",
        },
        legend: {
          data: ["设定温度(℃)", "测量温度(℃)", "湿度(%)"], // 不包含占位符系列
          textStyle: {
            color: "#000",
          },
        },
        grid: {
          // 控制绘图区域的位置和大小
          left: "3%",
          right: "4%",
          bottom: "3%",
          top: "20%",
          containLabel: true, // 是否包含坐标轴标签
        },
        xAxis: {
          type: "category",
          boundaryGap: false,
          data: this.chartData.xAxis,
          axisLabel: {
            color: "#9498a1", // X轴字体颜色
            margin: 10,
            fontSize: 11,
          },
          axisLine: {
            lineStyle: {
              color: "rgba(148, 163, 184, 0.2)",
            },
          },
        },
        yAxis: [
          // 修改为数组形式以支持双Y轴
          {
            type: "value",
            name: "温度(℃)",
            nameTextStyle: {
              color: "#9498a1",
              padding: [0, 0, 0, 40],
              fontSize: 11,
            },
            splitLine: {
              lineStyle: {
                color: "rgba(148, 163, 184, 0.1)",
              },
            },
            axisLabel: {
              color: "#9498a1", // Y轴字体颜色
              fontSize: 11,
            },
            axisTick: {
              show: false,
            },
            axisLine: {
              show: false, // 确保隐藏右侧Y轴竖线
              lineStyle: {
                color: "transparent", // 额外设置颜色为透明
              },
            },
          },
          {
            type: "value",
            name: "湿度(%)",
            nameTextStyle: {
              color: "#9498a1",
              padding: [0, 0, 0, 0],
              fontSize: 11,
            },
            splitLine: {
              show: false,
            },
            axisLabel: {
              color: "#9498a1",
              fontSize: 11,
              formatter: "{value}%",
            },
            axisTick: {
              show: false,
            },
            axisLine: {
              show: false, // 确保隐藏右侧Y轴竖线
              lineStyle: {
                color: "transparent", // 额外设置颜色为透明
              },
            },
          },
        ],
        series: [
          {
            name: "设定温度(℃)",
            data: this.chartData.series1,
            type: "line",
            symbolSize: 8,
            itemStyle: {
              color: "#FF9800", // 内部实心颜色
              borderColor: "#FF9800", // 白色边框
              borderWidth: 2, // 边框宽度
              borderType: "solid", // 实线边框
            },
            lineStyle: {
              color: "#FF9800", // 线条颜色
              width: 1,
              type: "dashed", // 修改为虚线
            },
            // areaStyle: {
            //   color: new echarts.graphic.LinearGradient(0, 0, 0, 0.8, [
            //     {
            //       offset: 0,
            //       color: "rgba(123, 135, 89, .9)",
            //     },
            //     {
            //       offset: 1,
            //       color: "rgba(123, 135, 89, .1)",
            //     },
            //   ]),
            // },
            smooth: true,
            showSymbol: true,
          },
          {
            name: "测量温度(℃)",
            data: this.chartData.series2,
            type: "line",
            symbolSize: 8,
            itemStyle: {
              color: "#2652c2", // 内部实心颜色
              borderColor: "#2652c2", // 白色边框
              borderWidth: 2, // 边框宽度
              borderType: "solid", // 实线边框
            },
            lineStyle: {
              color: "#2652c2", // 线条颜色
              width: 2,
            },
            // areaStyle: {
            //   color: new echarts.graphic.LinearGradient(0, 0, 0, 0.5, [
            //     {
            //       offset: 0,
            //       color: "rgba(123, 135, 89, .7)",
            //     },
            //     {
            //       offset: 1,
            //       color: "rgba(123, 135, 89, .1)",
            //     },
            //   ]),
            // },
            smooth: true,
            showSymbol: true,
          },
          {
            name: "湿度(%)",
            data: this.chartData.series3,
            type: "line",
            symbolSize: 8,
            itemStyle: {
              color: "#2cc190", // 内部实心颜色
              borderColor: "#2cc190", // 白色边框
              borderWidth: 2, // 边框宽度
              borderType: "solid", // 实线边框
            },
            lineStyle: {
              color: "#2cc190", // 线条颜色
              width: 2,
            },
            // areaStyle: {
            //   color: new echarts.graphic.LinearGradient(0, 0, 0, 0.5, [
            //     {
            //       offset: 0,
            //       color: "rgba(123, 135, 89, .7)",
            //     },
            //     {
            //       offset: 1,
            //       color: "rgba(123, 135, 89, .1)",
            //     },
            //   ]),
            // },
            smooth: true,
            showSymbol: true,
          },
          // 添加透明占位系列以确保右侧Y轴显示
          {
            name: "占位符",
            type: "line",
            yAxisIndex: 1,
            data: [0, 0, 0, 0, 0],
            itemStyle: {
              opacity: 0,
            },
            lineStyle: {
              opacity: 0,
            },
            symbol: "none",
            tooltip: {
              show: false,
            },
          },
        ],
      };

      this.chart.setOption(option);
    },

    resizeChart() {
      if (this.chart) {
        // 添加延迟确保容器尺寸已更新
        setTimeout(() => {
          this.chart.resize();
        }, 100);
      }
    },
  },
  watch: {
    // 监听数据变化，更新图表
    chartData: {
      handler() {
        if (this.chart) {
          this.setChartOption();
        }
      },
      deep: true,
    },
  },
};
</script>
<style scoped lang="scss">
@import "./styles/common.scss";
.temreture-analysis-wrapper.card-header {
  .head {
    .head-icon {
      background-color: #dbeafe;
      color: #2563eb;
    }
  }
  .data-energy-container {
    width: 100%;
    height: 300px;
    margin: 15px 0;
  }
  .electricity-consumption {
    width: 100%;
    color: #1e40af;
    background-color: rgba(239, 246, 255, 0.8);
    main {
      margin-top: 10px;
      .main-left,
      .main-right {
        width: 50%;
      }
    }
  }
}
</style>
